package qzbook.cn.config;

import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;
import java.util.Properties;


/**
 * @Description
 * @Author zc
 * @Date 2020/11/13 上午1:09
 */
@Configuration                  //声明当前类是一个配置类
@ComponentScan("qzbook.cn.*")   //spring包扫描路径
@EnableTransactionManagement    //事务管理器开启,

/**
 * 实现接口，配置注解驱动事务
 * */
public class AppConfig implements TransactionManagementConfigurer {

    private DataSource dataSource = null;

    /**
     * 配置数据库
     * @return 数据库连接池
     * */
    @Bean(name="dataSource")
    public DataSource initDataSource(){
        if (dataSource != null){
            return dataSource;
        }
        try {
            Properties props = new Properties();
            props.setProperty("driverClassName","com.mysql.jdbc.Driver");
            props.setProperty("url","jdbc:mysql://localhost:3306/cache?characterEncoding=utf8");
            props.setProperty("username","root");
            props.setProperty("password","qzbook");
            dataSource = BasicDataSourceFactory.createDataSource(props);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        DruidDataSource druidDataSource = new DruidDataSource();
//        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
//        druidDataSource.setUrl("jdbc:mysql://localhost:3306/cache?characterEncoding=utf8");
//        druidDataSource.setUsername("root");
//        druidDataSource.setPassword("qzbook");
//        dataSource = druidDataSource;
        return dataSource;
    }

    /**
     * 配置 SqlSessionFactoryBean
     * @return SqlSessionFactoryBean
     * */
    @Bean(name = "sqlSessionFactoryBean")
    public SqlSessionFactoryBean initSqlSessionFactoryBean(){
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(initDataSource());
        ClassPathResource resource = new ClassPathResource("mybatis/mybatis-config.xml");
        sessionFactoryBean.setConfigLocation(resource);

        return sessionFactoryBean;
    }

    /**
     * 通过自动扫描，发现MyBatis Mapper接口
     * @return Mapper扫描器
     * */
    @Bean
    public MapperScannerConfigurer initMapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        //扫描宝
        msc.setBasePackage("qzbook.cn.*");
        msc.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
        //区分注解扫描
        msc.setAnnotationClass(Repository.class);
        return msc;
    }


    /**
     * 实现接口方法，注册注解事务，当@Transactional使用的时候产生数据库事务
     * */
    @Override
    @Bean(name = "annotationDrivenTransactionManager")
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(initDataSource());
        return transactionManager;
    }
}
